<style lang="less">
    @import 'assets/less/common.less';
</style>
<template>
    <transition name="fade" mode="out-in">
        <router-view></router-view>
    </transition>
</template>
<script>
    import * as types from 'store/mutation-types'
    import * as constants from 'src/AppConstants'
    import cache from 'commons/cache'
    import utils from 'commons/utils'
    import EventBus from 'eventbusjs'

    import {mapState, mapActions} from 'vuex'

    export default {
        data () {
            return {}
        },
        computed: {
            ...mapState({}),
        },
        methods: {
            ...mapActions([
                'logout'
            ]),
            initApp() {
                if (!utils.isLogin()) {
                    return
                }
                let userInfo = cache.getValue(constants.CACHE_USER_INFO)
                let apiToken = cache.getValue(constants.CACHE_API_TOKEN)
                this.$store.commit(types.UPDATE_TOKEN, apiToken)
                this.$store.commit(types.UPDATE_USER_INFO, userInfo)
            },

            logoutHandler() {
                this.logout(true).then((resp) => {
                    this.$router.push("/")
                }).catch((err) => {
                    console.warn('退出登录失败')
                })
            }
        },

        created() {
            this.initApp()
            EventBus.addEventListener(constants.EVENT_LOGOUT_SUCCESS, this.logoutHandler)
        },

        mounted () {

        },

        destoryed() {
            EventBus.removeEventListener(constants.EVENT_LOGOUT_SUCCESS, this.logoutHandler)
        }
    }
</script>
